package com.example.docmanagement.controller;

import com.example.docmanagement.dto.request.PermissionCreateRequest;
import com.example.docmanagement.dto.request.PermissionQueryRequest;
import com.example.docmanagement.dto.request.PermissionUpdateRequest;
import com.example.docmanagement.dto.response.ApiResponse;
import com.example.docmanagement.dto.response.PageResponse;
import com.example.docmanagement.dto.response.PermissionResponse;
import com.example.docmanagement.dto.response.PermissionTreeResponse;
import com.example.docmanagement.service.PermissionAdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理后台 - 权限管理接口
 */
@RestController
@RequestMapping("/api/v1/admin/permissions")
@RequiredArgsConstructor
@Tag(name = "权限管理", description = "管理后台权限管理接口")
public class AdminPermissionController {

    private final PermissionAdminService permissionAdminService;

    @GetMapping
    @Operation(summary = "权限分页查询", description = "分页查询权限列表")
    @PreAuthorize("hasAuthority('permission:list') or hasRole('ADMIN')")
    public ApiResponse<PageResponse<PermissionResponse>> listPermissions(@ParameterObject @Valid PermissionQueryRequest request) {
        return ApiResponse.success(permissionAdminService.searchPermissions(request));
    }

    @GetMapping("/{id}")
    @Operation(summary = "权限详情", description = "获取权限详细信息")
    @PreAuthorize("hasAuthority('permission:view') or hasRole('ADMIN')")
    public ApiResponse<PermissionResponse> getPermission(
            @Parameter(description = "权限ID", required = true, example = "1") @PathVariable Long id) {
        return ApiResponse.success(permissionAdminService.getPermission(id));
    }

    @PostMapping
    @Operation(summary = "创建权限", description = "新增权限定义")
    @PreAuthorize("hasAuthority('permission:create') or hasRole('ADMIN')")
    public ApiResponse<PermissionResponse> createPermission(@Valid @RequestBody PermissionCreateRequest request) {
        return ApiResponse.success(permissionAdminService.createPermission(request), "权限创建成功");
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新权限", description = "更新权限基本信息")
    @PreAuthorize("hasAuthority('permission:update') or hasRole('ADMIN')")
    public ApiResponse<PermissionResponse> updatePermission(
            @Parameter(description = "权限ID", required = true, example = "1") @PathVariable Long id,
                                                            @Valid @RequestBody PermissionUpdateRequest request) {
        return ApiResponse.success(permissionAdminService.updatePermission(id, request), "权限更新成功");
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除权限", description = "删除权限定义")
    @PreAuthorize("hasAuthority('permission:delete') or hasRole('ADMIN')")
    public ApiResponse<Void> deletePermission(
            @Parameter(description = "权限ID", required = true, example = "1") @PathVariable Long id) {
        permissionAdminService.deletePermission(id);
        return ApiResponse.success(null, "权限删除成功");
    }

    @GetMapping("/tree")
    @Operation(summary = "权限树", description = "按模块或资源分组的权限树列表")
    @PreAuthorize("hasAuthority('permission:list') or hasRole('ADMIN')")
    public ApiResponse<List<PermissionTreeResponse>> getPermissionTree() {
        return ApiResponse.success(permissionAdminService.getPermissionTree());
    }
}


